package com.ruoyi.project.system.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.system.domain.*;
import com.ruoyi.project.system.mapper.CollegeMapper;
import com.ruoyi.project.system.mapper.CollegeStaffMapper;
import com.ruoyi.project.system.mapper.SysDeptMapper;
import com.ruoyi.project.system.service.ICollegeService;
import com.ruoyi.project.system.service.ICourseStudyService;
import com.ruoyi.project.system.service.IStudentService;
import com.ruoyi.project.system.service.ISysDeptService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 院校Controller
 * 
 * @author ruoyi
 * @date 2020-10-07
 */
@RestController
@RequestMapping("/system/college")
public class CollegeController extends BaseController
{
    @Autowired
    private ICollegeService collegeService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private IStudentService studentService;

    @Autowired
    private ICourseStudyService courseStudyService;

    @Autowired
    private CollegeStaffMapper collegeStaffMapper;


    @Autowired
    private CollegeMapper collegeMapper;

    /**
     * 查询院校列表
     */
    @PreAuthorize("@ss.hasPermi('system:college:list')")
    @GetMapping("/list")
    public TableDataInfo list(College college)
    {
        startPage();
        List<College> list = collegeService.selectCollegeList(college);
        return getDataTable(list);
    }

    /**
     * 查询为添加学校管理员院校列表
     */
    @PreAuthorize("@ss.hasPermi('system:college:list')")
    @GetMapping("/listXz")
    public TableDataInfo listXz(College college)
    {
        startPage();
        List<College> list = collegeMapper.selectCollegeXzList(college);
        return getDataTable(list);
    }

    /**
     * 导出院校列表
     */
    @PreAuthorize("@ss.hasPermi('system:college:export')")
    @Log(title = "院校", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(College college)
    {
        List<College> list = collegeService.selectCollegeList(college);
        ExcelUtil<College> util = new ExcelUtil<College>(College.class);
        return util.exportExcel(list, "college");
    }

    /**
     * 获取院校详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:college:query')")
    @GetMapping(value = "/{collegeId}")
    public AjaxResult getInfo(@PathVariable("collegeId") Integer collegeId)
    {
        return AjaxResult.success(collegeService.selectCollegeById(collegeId));
    }

    /**
     * 新增院校
     */
    @PreAuthorize("@ss.hasPermi('system:college:add')")
    @Log(title = "院校", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody College college)
    {
        // 验证是否存在这个院校名称
        College u = collegeService.selectCollegeByCollegeName(college.getCollegeName());
        if(u!=null){
            throw new CustomException("新增学校名称已存在！");
        }
        college.setCollegeCreateDt(new Date());

        return toAjax(collegeService.insertCollege(college));



    }

    /**
     * 修改院校
     */
    @PreAuthorize("@ss.hasPermi('system:college:edit')")
    @Log(title = "院校", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody College college)
    {
        college.setCollegeModifyDt(new Date());
        return toAjax(collegeService.updateCollege(college));
    }

    /**
     * 删除院校
     */
    @PreAuthorize("@ss.hasPermi('system:college:remove')")
    @Log(title = "院校", businessType = BusinessType.DELETE)
	@DeleteMapping("/{collegeIds}")
    public AjaxResult remove(@PathVariable Integer[] collegeIds)
    {
        return toAjax(collegeService.deleteCollegeByIds(collegeIds));
    }


    @Log(title = "院校管理", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:college:import')")
    @PostMapping("/importData")
    @Transactional
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<College> util = new ExcelUtil<College>(College.class);
        List<College> collegeList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = collegeService.importCollege(collegeList, updateSupport);
        return AjaxResult.success(message);
    }

    @GetMapping("/importTemplate")
    public AjaxResult importTemplate()
    {
        ExcelUtil<College> util = new ExcelUtil<College>(College.class);
        return util.importTemplateExcel("院校数据");
    }


    /**
     *  查询学校数量
     * @return
     */
    @GetMapping("/getSchoolList")
    public AjaxResult getSchoolList()
    {
        SysUser sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        if(sysUser.getUserId()==1){
            int size = collegeService.getSchoolList();
            return AjaxResult.success(size);
        }
        return AjaxResult.success(1);
    }

    /**
     *  查询学生数量
     * @return
     */
    @GetMapping("/getStudentList")
    public AjaxResult getStudentList()
    {
        SysUser sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        if(sysUser.getUserId()!=1){
            int size = collegeService.getStudentListCollegeId(sysUser.getDeptId());
            return AjaxResult.success(size);
        }
        int size = collegeService.getStudentList();
        return AjaxResult.success(size);
    }

    /**
     *  查询学生学习
     * @return
     */
    @GetMapping("/getStudyList")
    public AjaxResult getStudyList()
    {

        SysUser sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        if(sysUser.getUserId()!=1){
            int size = collegeService.getStudyListCollegeId(sysUser.getDeptId());
            return AjaxResult.success(size);
        }
        int size = collegeService.getStudyList();
        return AjaxResult.success(size);
    }


    /**
     * 获取课程累计学习次数图表里面所需要的值
     * @return
     */
    @GetMapping("/getSumStudyNum")
    public AjaxResult getSumStudyNum()
    {

        SysUser sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        if(sysUser.getUserId()!=1){
            Map<String,Object> map  = collegeService.getSumStudyNumCollegeId(sysUser.getDeptId());
            return AjaxResult.success(map);
        }
        Map<String,Object> map = collegeService.getSumStudyNum();

        return AjaxResult.success(map);
    }


    /**
     * 获取每日学习次数图表里面所需要的值
     * @return
     */
    @GetMapping("/getSumDayStudyNum")
    public AjaxResult getSumDayStudyNum()
    {

        SysUser sysUser = tokenService.getLoginUser(ServletUtils.getRequest()).getUser();
        if(sysUser.getUserId()!=1){
            Map<String,Object> map  = courseStudyService.getSumDayStudyNumCollegeId(sysUser.getDeptId());
            return AjaxResult.success(map);
        }

        Map<String,Object> map = courseStudyService.getSumDayStudyNum();

        return AjaxResult.success(map);
    }



    /**
     * 查询院校下教師列表
     */
    @GetMapping("/staffList")
    public TableDataInfo staffList(String id)
    {
        CollegeStaff collegeStaff = new CollegeStaff();
        collegeStaff.setCollegeId(Long.parseLong(id));
        List<CollegeStaff> list = collegeStaffMapper.selectCollegeStaffList(collegeStaff);
        return getDataTable(list);
    }



}
